﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Lamex.Library;
using Lamex.SystemManager.BLL;
using Lamex.Base;
using DevExpress.XtraGrid.Views.Grid;
using Lamex.ControlsUI;

namespace Lamex.SystemManager.UI
{
    public partial class frmSelUser : frmShow
    {
        public frmSelUser()
        {
            InitializeComponent();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            string sqlWhere = "";
            string sql = " select * from Sys_MyUser where 1=1 ";
            if (!string.IsNullOrEmpty(txtAccount.Text))
            {
                sqlWhere += " and Account='" + txtAccount.Text + "'";
            }
            if (!string.IsNullOrEmpty(txtUserName.Text))
            {
                sqlWhere += " and UserName like '%" + txtUserName.Text + "%'";
            }
            if (ucDept1.Text!="无")
            {
                sqlWhere += " and Dept_ID = '" + ucDept1.Value + "'";
            }
            if (!string.IsNullOrEmpty(ucCompany1.Text))
            {
                sqlWhere += " and Company_ID = '" + ucCompany1.Value + "'";
            }
            
            sql = sql + sqlWhere;
            DataTable dt = BaseFactory<TUser>.Instance.GetDataTableBySql(sql);
            if (dt.Rows.Count != 0)
            {


                dt.Columns.Add("IsSelected", System.Type.GetType("System.Boolean"));
                foreach (DataColumn dc in dt.Columns)
                {
                    if (dc.ColumnName == "IsSelected")
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            dr["IsSelected"] = "false";
                        }
                    }
                }
                //DoBindingSummaryGrid(dt);
                gcView.DataSource = dt;
            }
        }
        private Dictionary<string, string> m_SelectUserDict = new Dictionary<string, string>();
        public Dictionary<string, string> SelectDict
        {
            get
            {
                return m_SelectUserDict;
            }
            set
            {
                m_SelectUserDict = new Dictionary<string, string>(value);
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (gridView2.DataRowCount==0)
            {
                MessageBox.Show("没有选中数据！");
                return;
            }
            for (int i = 0; i < gcView .Views[0].RowCount; i++)
            {
                if (this.gridView2.GetRowCellValue(i, "IsSelected").ToString().ToLower() == "true")
                {
                    string ID = gridView2.GetRowCellDisplayText(i, "isid");
                    string Account = gridView2.GetRowCellDisplayText(i, "Account");
                    string UserName = gridView2.GetRowCellDisplayText(i, "UserName") ;
                    string displayname = string.Format("{0}（{1}）", UserName, Account);

                    if (!this.SelectDict.ContainsKey(ID))
                    {
                        this.SelectDict.Add(ID, displayname);
                    }
                }
            }
            RefreshData();

        }

       private void RefreshData()
        {
            this.flowLayoutPanel1.Controls.Clear();
            foreach (string key in SelectDict.Keys)
            {
                string info = SelectDict[key];
                if (!string.IsNullOrEmpty(info))
                {
                    ucUserName control = new ucUserName();
                    control.BindData(key, info);
                    control.OnDeleteItem += new ucUserName.DeleteEventHandler(control_OnDelete);
                    this.flowLayoutPanel1.Controls.Add(control);
                }
            }
            this.lblItemCount.Text = string.Format("当前选择【{0}】项目", SelectDict.Keys.Count);
        }
       void control_OnDelete(string ID)
       {
           if (SelectDict.ContainsKey(ID))
           {
               SelectDict.Remove(ID);
               RefreshData();
           }
       }

       private void btnFinish_Click(object sender, EventArgs e)
       {
           if (this.SelectDict.Count == 0)
           {
               MessageBox.Show("您还未选择人员");
               return;
           }

           this.DialogResult = System.Windows.Forms.DialogResult.OK;
       }

       private void btnClear_Click(object sender, EventArgs e)
       {
           this.SelectDict = new Dictionary<string, string>();
           RefreshData();
           //this.DialogResult = System.Windows.Forms.DialogResult.OK;
       }

       private void btnColse_Click(object sender, EventArgs e)
       {
           this.Close();
       }
    }
}
